//
//  SEGTrackPhotoView.m
//  Uhomebk
//
//  Created by mac on 2017/10/24.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "SEGTrackPhotoView.h"
#import "SEGShowPhotoView.h"
//#import "UIImage+MultiFormat.h"
@interface SEGTrackPhotoView()
{
    NSMutableArray *_picDataArray;//图片数据源
}


@end


@implementation SEGTrackPhotoView
-(instancetype)initWithFrame:(CGRect)frame data:(NSDictionary *)dict
{
    if (self = [super initWithFrame:frame]) {
        [self initRecordSettingWith:dict];
        self.backgroundColor = kColor_W;
       
    }
    
    return self;
}


-(void)initRecordSettingWith:(NSDictionary *)dict
{
     _picDataArray = [[NSMutableArray alloc] init];
    UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 11+12, 0,20)];
    titleLabel.text = [NSString stringWithFormat:@"%@:",dict[@"title"]];
    [titleLabel setFont:kFont_15];
    [titleLabel sizeToFit];
    [titleLabel setTextColor:kColor_1];
    titleLabel.tag = 111;
    [self addSubview:titleLabel];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame)+10, 12, 63, 63)];
    imageView.layer.cornerRadius = 4;
    imageView.layer.masksToBounds = YES;
    imageView.tag = 999;
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPicTapAction:)];
    [imageView addGestureRecognizer:tap];
    imageView.userInteractionEnabled = YES;
    [self addSubview:imageView];

     self.height = imageView.height+12;
    if ([SEGUtils checkAttrValueWith:dict[@"attrValue"]] == NO)
    {
       [imageView setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];
        imageView.userInteractionEnabled = NO;
        return;
    }

    //判断是否有图片
    NSString *attrValue = dict[@"attrValue"];
    NSArray* picArray = [SEGUtils arrayWithJsonString:attrValue];
    //非签名控件是以json数组形式下发
    if (picArray.count > 0) {
        [_picDataArray addObjectsFromArray:picArray];
    }
    //签名控件是以字符串形式下发
    else{
        [_picDataArray addObject:attrValue];
    }

    if (_picDataArray.count > 0) {
            //默认显示第一张
            [imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,_picDataArray[0]]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];
            UILabel *countLbl = [[UILabel alloc] initWithFrame:CGRectMake(48, 48, 15, 15)];
            [countLbl setTextColor:kColor_W];
            countLbl.font = kFont_11;
            countLbl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
            countLbl.text = [NSString stringWithFormat:@"%ld",(unsigned long)_picDataArray.count];
            countLbl.textAlignment = NSTextAlignmentCenter;
            [imageView addSubview:countLbl];
            countLbl.hidden = NO;
             if (_picDataArray.count == 1) {
              countLbl.hidden = YES;
             }
    }
    else{
        [imageView setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];
        imageView.userInteractionEnabled = NO;
    }
    
}

-(void)setIsDetailUIType:(BOOL)isDetailUIType
{
    if (isDetailUIType == YES) {
        
    }
    UILabel *title = [self viewWithTag:111];
    UIImageView *imageView = [self viewWithTag:999];
    title.frame = CGRectMake(0, 12, 75, 39);
    title.numberOfLines = 0;
    title.textColor = kColor_2;
    imageView.frame = CGRectMake(85, 12, 63, 63);
}

-(void)setIsLogApplyInfoUIType:(BOOL)isLogApplyInfoUIType
{
    if (isLogApplyInfoUIType == YES) {
        UILabel *title = [self viewWithTag:111];
        UIImageView *imageView = [self viewWithTag:999];
        title.frame = CGRectMake(15, 0, 70, 39);
        title.numberOfLines = 0;
        title.textColor = kColor_2;
        [title sizeToFit];
        imageView.frame = CGRectMake(100, 0, 63, 63);
    }
  
}


- (void)showPicTapAction:(UIGestureRecognizer*)recognizer
{
    if (_picDataArray.count == 0) {
        return;
    }
    SEGShowPhotoView *browseView = [[SEGShowPhotoView alloc] initWithImageUrls:_picDataArray writable:@"0"];
    [self.parentVC.navigationController pushViewController:browseView animated:NO];
}


@end
